<html>
<head><meta charset="utf-8"><title>How do I get the ctor of an enum variant? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20ctor.20of.20an.20enum.20variant.3F.html">How do I get the ctor of an enum variant?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="233854932"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20ctor%20of%20an%20enum%20variant%3F/near/233854932" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20ctor.20of.20an.20enum.20variant.3F.html#233854932">(Apr 09 2021 at 16:44)</a>:</h4>
<p>I have a DefId that's a <code>DefKind::Variant</code>. How do I get the associated DefKind::Ctor?</p>



<a name="233855361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20ctor%20of%20an%20enum%20variant%3F/near/233855361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20ctor.20of.20an.20enum.20variant.3F.html#233855361">(Apr 09 2021 at 16:47)</a>:</h4>
<p>ok <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/adt/struct.AdtDef.html#method.variant_with_ctor_id">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/adt/struct.AdtDef.html#method.variant_with_ctor_id</a> looks about right</p>



<a name="233860352"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20ctor%20of%20an%20enum%20variant%3F/near/233860352" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20ctor.20of.20an.20enum.20variant.3F.html#233860352">(Apr 09 2021 at 17:23)</a>:</h4>
<p>oh err I don't actually know how to get the AdtDef of a variant</p>



<a name="233860529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20ctor%20of%20an%20enum%20variant%3F/near/233860529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20ctor.20of.20an.20enum.20variant.3F.html#233860529">(Apr 09 2021 at 17:24)</a>:</h4>
<p>I feel like this should be simple :(</p>



<a name="233860872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20ctor%20of%20an%20enum%20variant%3F/near/233860872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20ctor.20of.20an.20enum.20variant.3F.html#233860872">(Apr 09 2021 at 17:27)</a>:</h4>
<p>well I came up with this hack but it feels way overcomplicated</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">        </span><span class="bp">self</span><span class="p">.</span><span class="n">is_constructor</span><span class="p">(</span><span class="n">did</span><span class="p">)</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="c1">// `expect_variant_res` requires having a Res; see if `item` is an enum variant instead</span>
<span class="w">            </span><span class="bp">self</span><span class="p">.</span><span class="n">def_kind</span><span class="p">(</span><span class="n">item</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">DefKind</span>::<span class="n">Variant</span><span class="w"> </span><span class="o">&amp;&amp;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Res</span>::<span class="n">Def</span><span class="p">(</span><span class="n">DefKind</span>::<span class="n">Variant</span><span class="p">,</span><span class="w"> </span><span class="n">item</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">variant</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">expect_variant_res</span><span class="p">(</span><span class="n">res</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="n">debug</span><span class="o">!</span><span class="p">(</span><span class="s">"checking if ctor {:?} is a diagnostic item (variant={:?})`"</span><span class="p">,</span><span class="w"> </span><span class="n">did</span><span class="p">,</span><span class="w"> </span><span class="n">adt</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="n">variant</span><span class="p">.</span><span class="n">ctor_def_id</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">did</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="233863017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20ctor%20of%20an%20enum%20variant%3F/near/233863017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20ctor.20of.20an.20enum.20variant.3F.html#233863017">(Apr 09 2021 at 17:42)</a>:</h4>
<p>and also didn't work :(</p>



<a name="233865910"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/How%20do%20I%20get%20the%20ctor%20of%20an%20enum%20variant%3F/near/233865910" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/How.20do.20I.20get.20the.20ctor.20of.20an.20enum.20variant.3F.html#233865910">(Apr 09 2021 at 18:02)</a>:</h4>
<p>oh well it turns out this was on the wrong track anyway, this is a <code>Variant</code>, not a <code>Ctor</code> <span aria-label="face palm" class="emoji emoji-1f926" role="img" title="face palm">:face_palm:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>